package com.reactive.demo.demo;

import com.reactive.demo.demo.handler.UserHandler;
import com.reactive.demo.demo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Hooks;
import reactor.netty.http.server.HttpServer;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

//        // 创建对象
//        UserService userService = new UserService();
//        UserHandler userHandler = new UserHandler(userService);
//        // 创建路由
//        RouterFunction<ServerResponse> route = RouterFunctions
//                .route(RequestPredicates.GET("/user/{id}").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::getById)
//                .andRoute(RequestPredicates.GET("/users").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::getAll);
//        // 路由和handler适配
//        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
//        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
//        // 创建服务器
//        HttpServer httpServer = HttpServer.create().port(8080);
//        httpServer.handle(adapter).bindNow().onDispose().block();
//        Hooks.onOperatorDebug();
        SpringApplication.run(DemoApplication.class, args);
    }

}
